<?php 

if($error){
?><div align="center">
<div class="ui-widget ui-widget-content ui-corner-all" style="width: 30%;padding: 2px;">
	<h3>
	<?php echo __("Invalid hash. If you requested more than one activation email please note that only the last is valid");?>
	</h3>
</div></div>
<?php }else{
?><input type="hidden" id="hash" value="<?php echo $hash;?>">
<div align="center">
<div class="ui-widget ui-widget-content ui-corner-all" style="width: 30%;padding: 2px;">
	<h3><?php echo __("Complete your sign up in Wixet");?></h3>
	<div class="sub">
		<table>
			<tr><td><?php echo __("Firstname");?></td><td><input id="firstname" value="<?php echo $firstname;?>" type="text" class="check ui-widget-content ui-corner-all"></td></tr>
			<tr><td><?php echo __("Lastname");?></td><td><input id="lastname" value="<?php echo $lastname;?>" type="text" class="check form ui-widget-content ui-corner-all"></td></tr>
			<tr><td><?php echo __("Country");?></td><td><img class="listaFotos ui-state-default ui-corner-all" src="/images/flags/<?php echo strtolower($country); ?>.png" />&nbsp;<?php echo format_country($country); ?></td></tr>
			<tr><td><?php echo __("Email");?></td><td><?php echo $email;?></td></tr>
			<tr><td><?php echo __("Username");?></td><td><div><input id="username" value="<?php echo $username;?>" type="text" class="ui-widget-content ui-corner-all"><span id="usernamestat" style="float:right" class="ui-icon ui-icon-check"></span></div></td></tr>
			<tr><td><?php echo __("Password");?></td><td><input id="pass1" type="password" class="check text ui-widget-content ui-corner-all"></td></tr>
			<tr><td><?php echo __("Confirm password");?></td><td><input id="pass2" type="password" class="check text ui-widget-content ui-corner-all"></td></tr>
			<tr><td><?php echo __("Birthday");?></td><td><div id="birthday"></div></td></tr>
		</table>
		<br/>
		<button>Crear usuario</button>
	</div>
	<br>
</div>
</div>
<script>
$(document).ready(function(){
	$("#username").blur(function(){
		var input = $(this);
		$.getJSON("/core/account/usernameExists/username/"+input.val(),function(data){
			if(data == false){
				$("#usernamestat").removeClass("ui-icon-closethick").addClass("ui-icon-check");
				input.removeClass("ui-state-error");
			}
			else{
				$("#usernamestat").removeClass("ui-icon-check").addClass("ui-icon-closethick");
				input.addClass("ui-state-error");
				jAlert("Ese usuario ya existe. Por favor selecciona otro","Error");
			}
		});
	});
	$("#username").parent().width($("#username").width()+20);
$("button").button().click(function(){
	$(".check").removeClass("ui-state-error");
	var firstname = $.trim($("#firstname").val()),
		lastname = $.trim($("#lastname").val()),
		username = $.trim($("#username").val()),
		birthday = $("#birthday").datepicker('getDate');
		pass1 = $.trim($("#pass1").val()),
		pass2 = $.trim($("#pass2").val());

	if($("#username").hasClass("ui-state-error")){
		jAlert("Por favor escribe un nombre de usuario correcto","Error");
		$("#username").focus();
	}else if(firstname.length == 0){
		jAlert("Por favor escribe un nombre","Error");
		$("#firstname").addClass("ui-state-error").focus();
	}
	else if(lastname.length == 0){
		jAlert("Por favor escribe un apellido","Error");
		$("#lastname").addClass("ui-state-error").focus();
	}
	else if(pass1.length == 0 || pass1!=pass2){
		jAlert("Los campos de contrase&ntilde;a no coinciden","Error");
		$("#pass1").addClass("ui-state-error").focus();
		$("#pass2").addClass("ui-state-error");
	}
	else if($("#username").hasClass("ui-state-error"))
		jAlert("Por favor escoge un nombre de usuario","Error");
	else{
		var today = new Date();
		years = today.getFullYear()-birthday.getFullYear();
		var ageError = false;
		if(years < 14)
			ageError = true;
		else if(years == 14){
			if(birthday.getMonth()>today.getMonth())
				ageError = true;
		}
	
		if(ageError)
			jAlert("Tienes que tener m&aacute;s de 14 a&ntilde;os para poder registrarte","Error");
		else{//No error
			dialog = $('<div title="Creando cuenta"><br/><div align="center"><img src="/images/progress.gif"></div></div>');

			dialog.dialog({
				height: 100,
				modal: true,
				resizable: false,
				draggable: false
			});
			
			$(".ui-dialog").find(".ui-dialog-titlebar-close").remove();
			$.post("/core/account/newUser",{date:birthday.getFullYear()+"-"+(birthday.getMonth()+1)+"-"+birthday.getDate(), firstname:firstname,lastname:lastname,username:username,password:pass1,hash:$("#hash").val()},function(data){
				data = eval("("+data+")");
				if(data.error){
					dialog.dialog('close');
					jAlert(data.message,"Error");
				}
				else{
					dialog.dialog('close');
					jAlert("Usuario creado satisfactoriamente","Usuario creado",function(){
						location.href="/";
					});
				}
			});
		}
	}


});
$("#birthday").datepicker({
		changeMonth: true,
		changeYear: true,
		yearRange: '1900:2010',
		monthNamesShort: ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Sep.','Octubre','Nov.','Dic.'],
		firstDay:1,
		dayNamesMin: ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab']
		});
});
</script>
<?php }?>
